package com.meichis.yndms.repository.manager;

import android.util.ArrayMap;

import androidx.lifecycle.MutableLiveData;

import com.google.gson.reflect.TypeToken;
import com.meichis.mcsappframework.utils.MessageTools;
import com.meichis.yndms.http.CallSubscriber;
import com.meichis.yndms.http.IWebServiceCallback;
import com.meichis.yndms.http.RetrofitUtil.RxSchedulersHelper;
import com.meichis.yndms.model.BusinessBoard;
import com.meichis.yndms.repository.ApiUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xmy
 * @title
 * @remake
 * @create 2021-04-14 22:51
 */
public class BusinessBoardManager extends BaseManager {

    private static BusinessBoardManager INSTANCE;

    public static BusinessBoardManager getInstance() {
        if (INSTANCE == null) {
            synchronized (BusinessBoardManager.class) {
                if (INSTANCE == null) {
                    INSTANCE = new BusinessBoardManager();
                }
            }
        }
        return INSTANCE;
    }

    public void getMonthProductStandingByBrand(final MutableLiveData<Boolean> showDialog, String partnerId, String staffId, MutableLiveData<List<BusinessBoard>> data) {
        showDialog.setValue(true);
        ArrayMap<String, Object> hs = new ArrayMap<>();
        //MDS经销商ID
        hs.put("partnerId", partnerId);
        hs.put("staffId", staffId);
        getRootApi().getMonthProductStandingByBrand(getToken(), ApiUtil.getRequestPack(hs))
                .compose(RxSchedulersHelper.rxSchedulerHelper())
                .subscribe(new CallSubscriber(new TypeToken<List<BusinessBoard>>() {
                }.getType(), new IWebServiceCallback<List<BusinessBoard>>() {
                    @Override
                    public void onSucceed(int sequence, List<BusinessBoard> resultPack, String ReturnInfo, int Return) {
                        if (resultPack == null) {
                            MessageTools.showLongToast("暂无数据！");
                            data.setValue(new ArrayList<>());
                        } else {
                            data.setValue(resultPack);
                        }
                        showDialog.setValue(false);
                    }

                    @Override
                    public void onFailure(int sequence, int errorCode, String error) {
                        showDialog.setValue(false);
                        MessageTools.showLongToast(error);
                    }
                }));

    }

    public void getMonthProductSalesSummaryByBrand(final MutableLiveData<Boolean> showDialog, String partnerId, String staffId, MutableLiveData<List<BusinessBoard>> data) {
        showDialog.setValue(true);
        ArrayMap<String, Object> hs = new ArrayMap<>();
        //MDS经销商ID
        hs.put("partnerId", partnerId);
        hs.put("staffId", staffId);
        getRootApi().getMonthProductSalesSummaryByBrand(getToken(), ApiUtil.getRequestPack(hs))
                .compose(RxSchedulersHelper.rxSchedulerHelper())
                .subscribe(new CallSubscriber(new TypeToken<List<BusinessBoard>>() {
                }.getType(), new IWebServiceCallback<List<BusinessBoard>>() {
                    @Override
                    public void onSucceed(int sequence, List<BusinessBoard> resultPack, String ReturnInfo, int Return) {
                        if (resultPack == null) {
                            MessageTools.showLongToast("暂无数据！");
                            data.setValue(new ArrayList<>());
                        } else {
                            data.setValue(resultPack);
                        }
                        showDialog.setValue(false);
                    }

                    @Override
                    public void onFailure(int sequence, int errorCode, String error) {
                        showDialog.setValue(false);
                        MessageTools.showLongToast(error);
                    }
                }));

    }

    public void getMonthProductArrivalByBrand(final MutableLiveData<Boolean> showDialog, String partnerId, String staffId, MutableLiveData<List<BusinessBoard>> data) {
        showDialog.setValue(true);
        ArrayMap<String, Object> hs = new ArrayMap<>();
        //MDS经销商ID
        hs.put("partnerId", partnerId);
        hs.put("staffId", staffId);
        getRootApi().getMonthProductArrivalByBrand(getToken(), ApiUtil.getRequestPack(hs))
                .compose(RxSchedulersHelper.rxSchedulerHelper())
                .subscribe(new CallSubscriber(new TypeToken<List<BusinessBoard>>() {
                }.getType(), new IWebServiceCallback<List<BusinessBoard>>() {
                    @Override
                    public void onSucceed(int sequence, List<BusinessBoard> resultPack, String ReturnInfo, int Return) {
                        if (resultPack == null) {
                            MessageTools.showLongToast("暂无数据！");
                            data.setValue(new ArrayList<>());
                        } else {
                            data.setValue(resultPack);
                        }
                        showDialog.setValue(false);
                    }

                    @Override
                    public void onFailure(int sequence, int errorCode, String error) {
                        showDialog.setValue(false);
                        MessageTools.showLongToast(error);
                    }
                }));

    }

    public void getMonthProductPurchaseByBrand(final MutableLiveData<Boolean> showDialog, String partnerId, String staffId, MutableLiveData<List<BusinessBoard>> data) {
        showDialog.setValue(true);
        ArrayMap<String, Object> hs = new ArrayMap<>();
        //MDS经销商ID
        hs.put("partnerId", partnerId);
        hs.put("staffId", staffId);
        getRootApi().getMonthProductPurchaseByBrand(getToken(), ApiUtil.getRequestPack(hs))
                .compose(RxSchedulersHelper.rxSchedulerHelper())
                .subscribe(new CallSubscriber(new TypeToken<List<BusinessBoard>>() {
                }.getType(), new IWebServiceCallback<List<BusinessBoard>>() {
                    @Override
                    public void onSucceed(int sequence, List<BusinessBoard> resultPack, String ReturnInfo, int Return) {
                        if (resultPack == null) {
                            MessageTools.showLongToast("暂无数据！");
                            data.setValue(new ArrayList<>());
                        } else {
                            data.setValue(resultPack);
                        }
                        showDialog.setValue(false);
                    }

                    @Override
                    public void onFailure(int sequence, int errorCode, String error) {
                        showDialog.setValue(false);
                        MessageTools.showLongToast(error);
                    }
                }));

    }


}
